/**   
* @Title: IndexServiceImpl.java 
* @Package com.kerrykidz.system.service.impl 
* @Description: 
* Copyright: Copyright (c) 2014 
* Company:wuwh team by iss
* @author: zhuss   
* @date: 2014年9月25日 上午10:44:37 
* @version: V1.0
*
*/
package com.kerrykidz.system.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.filemanagement.dao.mapper.LocPerInoutEntityMapper;
import com.kerrykidz.membership.dao.mapper.CusMemEntityMapper;
import com.kerrykidz.membership.dao.model.CusMemEntity;
import com.kerrykidz.parreserve.dao.mapper.ParReserveEntityMapper;
import com.kerrykidz.parreserve.dao.model.ParReserveEntity;
import com.kerrykidz.scminvoicing.dao.mapper.ScmPrdStgEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.ScmPrdStgEntity;
import com.kerrykidz.system.dao.mapper.SecRole2usrEntityMapper;
import com.kerrykidz.system.service.IIndexService;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.vo.IndexVo;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.system.vo.WarnVo;
import com.kerrykidz.workflow.dao.mapper.ActivitiEntityMapper;
import com.kerrykidz.workflow.dao.model.ActivitiEntity;
import com.kerrykidz.workflow.vo.SearchParamVo;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.config.PropertiesUtil;

/**
 * 
 * @ClassName: IndexServiceImpl 
 * @description: 首页实现类
 * @author: zhuss 
 * @date: 2014年9月25日 上午10:44:42 
 * @version: V1.0
 *
 */
@Service
public class IndexServiceImpl implements IIndexService{

	@Autowired
	SecRole2usrEntityMapper secRole2usrEntityMapper;
	
	@Autowired
	ScmPrdStgEntityMapper scmPrdStgMapper;
	
	@Autowired
	CusMemEntityMapper cusMemMapper;
	
	@Autowired
	ParReserveEntityMapper parReserveMapper;
	
	@Autowired
	LocPerInoutEntityMapper locPerInoutMapper;
	
	@Autowired
	ActivitiEntityMapper activitiMapper;
	
	@Autowired
	DataSourceUtil das;
	
	/**
	 * 查询首页数据
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月25日
	 */
	@Override
	public IndexVo queryIndex(UserInfo user) throws RDPException {
		IndexVo vo = new IndexVo();
		SearchParamVo searchParam = new SearchParamVo();
		searchParam.setUserInfo(user);
		searchParam.setType("0");
		//List<SecRole2usrEntity> ruls=  secRole2usrEntityMapper.findUserRoleById(user.getUserId());
		if(!user.getRoleList().contains("103")){
			searchParam.setOptUserId(user.getUserId());
		}
		if(StringUtils.isNotBlank(user.getBranId())){
			vo.setIsBranch("0");
			if(user.getBranId().indexOf("ZB") == -1){//如果是门店的话
				vo.setIsBranch("1");
				//待确认的派对团体(五日内将要举办的派对团体)
				List<ParReserveEntity> noconfirm = queryNoConfirm(searchParam);
				if(null != noconfirm && noconfirm.size() > 5)
					vo.setIndexNoConfirm(noconfirm.subList(0, 5));
				else
					vo.setIndexNoConfirm(noconfirm);
				
				//预约后24小时未预付的团体
				List<ParReserveEntity> nopay = queryNoPay(searchParam);
				if(null != nopay && nopay.size() > 5)
					vo.setIndexNoPay(nopay.subList(0, 5));
				else
					vo.setIndexNoPay(nopay);
				if(StringUtils.isNotBlank(user.getJobLvl())){
					if(user.getJobLvl().equals("3")){//如果是门店经理的话
						searchParam.setOptUserId(null);
					}
					//即将到期的会员
					List<CusMemEntity> endMem = queryEndMem(searchParam);
					//近期生日会员
					List<CusMemEntity> birthdayMem = queryBirthdayMem(searchParam);
					//即将到期的会员
					if(null != endMem && endMem.size() > 5)
						vo.setIndexEndMem(endMem.subList(0, 5));
					else
						vo.setIndexEndMem(endMem);
					//近期生日会员
					if(null != birthdayMem && birthdayMem.size() > 5)
						vo.setIndexBirthdayMem(birthdayMem.subList(0, 5));
					else
						vo.setIndexBirthdayMem(birthdayMem);
				}
				//场内人数
				PropertiesUtil prop = new PropertiesUtil();
				String num = prop.readSingleProps("MAX_FLOORNUM", "/properties/kerrykidz.properties");
				if(StringUtils.isNotBlank(num))
					vo.setDefailFloorNum(Integer.valueOf(num));
				vo.setFloorNum(locPerInoutMapper.indexFloorNum());
				
				//库存数量
				List<ScmPrdStgEntity> scmCount = queryScmCount(searchParam);
				//近期生日会员
				if(null != scmCount && scmCount.size() > 5)
					vo.setIndexScmCount(scmCount.subList(0, 5));
				else
					vo.setIndexScmCount(scmCount);
			}
			searchParam.setType("");
			//我的待办任务
			List<ActivitiEntity> todoList = queryTodoTask(searchParam);
			if(null != todoList && todoList.size() > 5)
				vo.setIndexTodoTask(todoList.subList(0, 5));
			else
				vo.setIndexTodoTask(todoList);
			//我的已办
			List<ActivitiEntity> finishList = queryFinishTask(searchParam);
			if(null != finishList && finishList.size() > 5)
				vo.setIndexFinishTask(finishList.subList(0, 5));
			else
				vo.setIndexFinishTask(finishList);
		}
		return vo;
	}
	
	/**
	 * 待确认的派对团体（5日内将要举办的派对团体）
	 *
	 * @param: @param searchParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<ParReserveEntity> queryNoConfirm(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fkBranId", searchParam.getUserInfo().getBranId());
		map.put("order", searchParam.getOrder());
		searchParam.setParams(map);
		return parReserveMapper.indexNoConfirm(searchParam);
	}

	/**
	 * 预约后24小时内未预付的团体
	 *
	 * @param: @param searchParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<ParReserveEntity> queryNoPay(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fkBranId", searchParam.getUserInfo().getBranId());
		map.put("order", searchParam.getOrder());
		map.put("fkSales", searchParam.getOptUserId());
		searchParam.setParams(map);
		return parReserveMapper.indexNoPay(searchParam);
	}

	/**
	 * 即将到期会员
	 *
	 * @param: @param searchParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<CusMemEntity> queryEndMem(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fkBranId", searchParam.getUserInfo().getBranId());
		//map.put("order", searchParam.getOrder());
		map.put("fkSales", searchParam.getOptUserId());
		searchParam.setParams(map);
		return cusMemMapper.indexEndMem(searchParam);
	}

	/**
	 * 近期生日会员
	 *
	 * @param: @param searchParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<CusMemEntity> queryBirthdayMem(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fkBranId", searchParam.getUserInfo().getBranId());
		map.put("order", searchParam.getOrder());
		searchParam.setParams(map);
		return cusMemMapper.indexBirthdayMem(searchParam);
	}
	
	/**
	 * 库存数量
	 *
	 * @param: @param searchParam
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<ScmPrdStgEntity> queryScmCount(SearchParamVo searchParam)
			throws RDPException {
		return scmPrdStgMapper.indexScmCount(searchParam.getUserInfo().getBranId(),searchParam.getType());
	}

	/**
	 * 
	 * 查询场内人数
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public WarnVo indexFloorNum() throws RDPException {
		WarnVo vo = new WarnVo();
		//设置的最大人数
		PropertiesUtil prop = new PropertiesUtil();
		String num = prop.readSingleProps("MAX_FLOORNUM", "/properties/kerrykidz.properties");
		if(StringUtils.isNotBlank(num))
			vo.setDefailFloorNum(Integer.valueOf(num));
		vo.setFloorNum(locPerInoutMapper.indexFloorNum());
		return vo;
	}
	
	/**
	 * 查询预警库存
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月26日
	 */
	@Override
	public WarnVo indexWarnScm(SearchParamVo searchParam)throws RDPException{
		WarnVo vo = new WarnVo();
		vo.setIndexScmCount(queryScmCount(searchParam));
		return vo;
	}

	/**
	 * 我的待办
	 *
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<ActivitiEntity> queryTodoTask(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", searchParam.getUserInfo().getUserId());
		map.put("order", searchParam.getOrder());
		map.put("type", searchParam.getType());
		searchParam.setParams(map);
		List<ActivitiEntity> todoList = das.queryTodoTask(searchParam);
		if(null != todoList){
			int sumc = das.queryTodoTaskCount(searchParam);
			searchParam.setTableId("table1searchForm");
			searchParam.setTotalRecord(sumc);
			return todoList;
		}else
			return activitiMapper.indexTodoTask(searchParam);
	}

	/**
	 * 我的已办
	 *
	 * @param: @return
	 * @param: @throws RDPException
	 * @return: IndexServiceImpl
	 * @throws
	 */
	@Override
	public List<ActivitiEntity> queryFinishTask(SearchParamVo searchParam) throws RDPException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", searchParam.getUserInfo().getUserId());
		map.put("order", searchParam.getOrder());
		map.put("type", searchParam.getType());
		searchParam.setParams(map);
		List<ActivitiEntity> finishList = das.queryFinishTask(searchParam);
		if(null != finishList){
			int sumc = das.queryFinishTaskCount(searchParam);
			searchParam.setTableId("table1searchForm");
			searchParam.setTotalRecord(sumc);
			return finishList;
		}else
			return activitiMapper.indexFinishTask(searchParam);
	}

}
